﻿function adv_DuiBaiXianShi(arg1)
{
	$GLOBAL_DIALOG_LOG=jqxx_DuQu("log")
	ajax(
	{
		type:"GET",
		url:"script/__config.advs",
		success:function(data){
				$GLOBAL_CONFIG=JSON.parse(data)
				ajax(
	{
		type: "GET",
		url: "script/" + arg1 + ".advs",
		success: function(data)
		{
			adv_ShengChengFenQu(data);
			$GLOBAL_DIALOG = adv_DuiBai(data);
			document.getElementById("title").innerHTML = adv_BiaoTi(data);
			//console.log(data)
			//console.log(adv_DuiBai(data))
			$GLOBAL_SLT = adv_XuanZe(data);
			$GLOBAL_NUMBER = -1;
			$GLOBAL_BASIC_SCRIPT=adv_JiaoBen(data)
			if($GLOBAL_BASIC_SCRIPT=="")
			{
				adv_GengXin()
			}
			else
			{
				$GLOBAL_BASIC_FLAG = true;
				$GLOBAL_BASIC_SCRIPT=basic_split($GLOBAL_BASIC_SCRIPT)
				$GLOBAL_BASIC_LINE=0
				basic_exec($GLOBAL_BASIC_SCRIPT);
			}
		}
		}
	)
		}
	}
	)
	

}
function adv_GengXin()
{
	if($GLOBAL_BASIC_FLAG === false)
		{
			if ($GLOBAL_NUMBER === $GLOBAL_DIALOG.length - 1)
	{
		clearInterval($GLOBAL_INTERVAL);
		adv_XuanZeXianShi();
	}
	else{
		$GLOBAL_NUMBER++;
		while ($GLOBAL_DIALOG[$GLOBAL_NUMBER]["character"] === ""&&$GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"] === "")
		{
			$GLOBAL_NUMBER++;
		}
		document.getElementById("cName").innerHTML = $GLOBAL_DIALOG[$GLOBAL_NUMBER]["character"];
		$GLOBAL_DIALOG_LOG+=$GLOBAL_DIALOG[$GLOBAL_NUMBER]["character"]+$GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"];
		if(typeof ($GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"])!="undefined")
		{
		clearInterval($GLOBAL_INTERVAL);
		document.getElementById("Dlg").innerHTML="";
		//document.getElementById("Dlg").innerHTML=($GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"]===undefined)?"":$GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"];
		$GLOBAL_PLACE = 0;
		$GLOBAL_INTERVAL=setInterval(adv_DaZi, adv_MoRenZhi($GLOBAL_CONFIG.typingSpeed,50));
		}}
		}
		else
		{
			$GLOBAL_BASIC_LINE++;
			basic_exec($GLOBAL_BASIC_SCRIPT);
		}
	
}
function jqxx_TiaoZhuan(webpage)
{
	window.location.href = webpage;
}
function adv_XuanZeXianShi()
{
	main.onclick=""
	main.innerHTML = "";
	jqxx_CunChu("log",$GLOBAL_DIALOG_LOG)
	for (var i = 0; i < $GLOBAL_SLT.length; i++)
		main.innerHTML += `<p><button onclick="jqxx_TiaoZhuan('?dialogue=${$GLOBAL_SLT[i].script}')" id="${$GLOBAL_SLT[i].selection}" class="option">${$GLOBAL_SLT[i].selection}</button></p>`;
}
function adv_DaZi()
{
	var dlg = $GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"];

	var plc = $GLOBAL_PLACE;
	//console.log("scan to ",dlg[plc]);
	//document.getElementById("Dlg").innerHTML+=$GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"][$GLOBAL_PLACE]+$GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"][$GLOBAL_PLACE+1]+$GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"][$GLOBAL_PLACE+2]+$GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"][$GLOBAL_PLACE+3]+$GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"][$GLOBAL_PLACE+4]+$GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"][$GLOBAL_PLACE+5];
	if (dlg[plc] === "&" && dlg[plc + 5] === ";")
	{
		document.getElementById("Dlg").innerHTML += dlg[plc] + dlg[plc + 1] + dlg[plc + 2] + dlg[plc + 3] + dlg[plc + 4] + dlg[plc + 5];
		$GLOBAL_PLACE+=6
	}
	else 	if (dlg[plc] === "&")
	{
		document.getElementById("Dlg").innerHTML += dlg[plc] + dlg[plc + 1] + dlg[plc + 2] + dlg[plc + 3] + dlg[plc + 4] ;
		$GLOBAL_PLACE+=5
	}
	else if (dlg[plc] === "<")
	{
		//console.log("p", plc)
		var temp = "";
		var i = plc;
		while (dlg[i] != ">" || (!(dlg[i - 1] == "\\") && (dlg[i - 1] == ">")))
		{
			temp += dlg[i];
			i++;
		}
		temp += dlg[i]
		//console.log(temp)
		document.getElementById("Dlg").innerHTML += temp;
		$GLOBAL_PLACE = i + 1;
	}
	else
	{
		document.getElementById("Dlg").innerHTML += dlg[plc];
		$GLOBAL_PLACE += 1;
	}
	
	if ($GLOBAL_PLACE >= $GLOBAL_DIALOG[$GLOBAL_NUMBER]["dialogue"].length )
	{
		if($GLOBAL_MODE==1)
		{
			setTimeout(adv_GengXin,3000);
		}
		else
		{
			if($GLOBAL_MODE==2)
			{
			setTimeout(adv_GengXin,2000);
			}
			else
			{
				if($GLOBAL_MODE==3)
				{
								setTimeout(adv_GengXin,1000);
				}
				else
				{
					if($GLOBAL_MODE==4)
					{
						setTimeout(adv_GengXin,500);
					}
				}
			}
		}
		$GLOBAL_PLACE = 0;
		clearInterval($GLOBAL_INTERVAL);
		return;
	}
}
function adv_ZiDong()
{
	
	if($GLOBAL_MODE==0)
	{
		$GLOBAL_MODE=1;
		autoExecute.innerHTML="自动1x";
		nextDialogue.style.display="none";
	}
	else
	{
		if($GLOBAL_MODE==1)
		{
			$GLOBAL_MODE=2;
			autoExecute.innerHTML="自动2x";	
			//nextDialogue.style.display="none";
		}
		else
		{
			if($GLOBAL_MODE==2)
			{
							$GLOBAL_MODE=3;
			autoExecute.innerHTML="自动3x";	
			nextDialogue.style.display="none";
			}
			else
			{
			if($GLOBAL_MODE==2)
			{
							$GLOBAL_MODE=4;
			autoExecute.innerHTML="自动4x";	
			nextDialogue.style.display="none";
			}
			else
			{
			$GLOBAL_MODE=0;
			autoExecute.innerHTML="自动 关";
			nextDialogue.style.display="inline";
			}
			}
		}
	}
adv_GengXin();
}
function adv_XiangQian()
{
	if($GLOBAL_NUMBER>=1)
	{
	 $GLOBAL_NUMBER-=2;
	 adv_GengXin();
	}
}